package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.Pms_Brand_Mapper;
import com.shop.pojo.pms.Pms_Brand;
import com.shop.service.Pms_Brand_Service;
import com.shop.utils.LayuiUtils;
import com.shop.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class Pms_Brand_ServiceImpl implements Pms_Brand_Service {

    @Autowired
    private Pms_Brand_Mapper pms_brand_mapper;

    /*
     * @Description 根据商品分类id去查询当前分类下的全部品牌信息
     * @param catelog_id
     * @return java.util.List<com.shop.pojo.pms.Pms_Brand>
     * @author liusupeng
     * @date 2020/10/31 15:30
     */
    @Override
    public R findAllBrandByCatalogId(Integer catelog_id) {
        R r1 = new R();
        r1.data(pms_brand_mapper.findAllBrandByCatalogId(catelog_id));
        return r1;
    }


    /**
     * 查询全部品牌信息进行显示
     *
     * @param pages
     * @param pms_brand
     * @return
     */
    @Override
    public LayuiUtils<Pms_Brand> find_admin_Brand_All(Page<Pms_Brand> pages, Pms_Brand pms_brand) {
        LayuiUtils<Pms_Brand> layuis = new LayuiUtils<>();

        QueryWrapper<Pms_Brand> query = new QueryWrapper<>();
        if (pms_brand.getName() != null) {
            query.like("name", pms_brand.getName());
        }

        Page<Pms_Brand> pms_brandPage = pms_brand_mapper.selectPage(pages, query);

        layuis.setCount(pms_brandPage.getTotal());
        layuis.setData(pms_brandPage.getRecords());

        return layuis;
    }

    /**
     * 添加品牌方法
     *
     * @param pms_brand
     * @return
     */
    @Override
    public R insert_admin_All(Pms_Brand pms_brand) {
        QueryWrapper<Pms_Brand> queryWrapper = new QueryWrapper<>();
        if (pms_brand.getName() != null){
            queryWrapper.eq("name",pms_brand.getName());
        }
        List<Pms_Brand> pms_brands = pms_brand_mapper.selectList(queryWrapper);

        if (pms_brands.size() == 0){
            int insert = pms_brand_mapper.insert(pms_brand);

            if (insert > 0) {
                return R.success();
            }
            return R.error(500, "添加失败");
        }

        return R.error(500,"品牌名称重复");

    }

    /**
     * 删除品牌信息
     *
     * @param brand_id
     * @return
     */
    @Override
    public R delete_admin_All(List<String> brand_id) {

        int ids1 = pms_brand_mapper.deleteBatchIds(brand_id);
        if (ids1 > 0) {
            return R.success();
        } else {
            return R.error(500, "删除失败");
        }
    }

    @Override
    public R up_admin_All(Pms_Brand pms_brand) {

        int i = pms_brand_mapper.updateById(pms_brand);
        if (i > 0) {
            return R.success();
        } else {
            return R.error(500, "失败");
        }
    }


}
